﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;

namespace DAO
{
    public class TuyenBay_DAO
    {
        public static TuyenBay_DTO CreateTuyenBayDTO(TuyenBay tuyenbay)
        {
            TuyenBay_DTO tuyenbayDTO = new TuyenBay_DTO();
            Mapper.CreateMap<TuyenBay, TuyenBay_DTO>();
            tuyenbayDTO = Mapper.Map<TuyenBay, TuyenBay_DTO>(tuyenbay);
            return tuyenbayDTO;
        }

        //public static TuyenBay CreateTuyenBayEFF(TuyenBay_DTO tuyenbayDTO)
        //{
        //    TuyenBay tuyenbay = new TuyenBay();
        //    tuyenbay.MaTuyenBay = tuyenbayDTO.MaTuyenBay;
        //    //tuyenbay.TenTuyenBay = tuyenbayDTO.TenTuyenBay;
        //    tuyenbay.SanBayDi = tuyenbayDTO.SanBayDi;
        //    tuyenbay.SanBayDen = tuyenbayDTO.SanBayDen;
        //    return tuyenbay;
        //}

        public List<TuyenBay_DTO> GetTuyenBayList()
        {
            List<TuyenBay_DTO> listtuyenbay = new List<TuyenBay_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from tb in context.TuyenBays select tb;
                foreach (var tuyenbay in query)
                {
                    TuyenBay_DTO tuyenbayDTO = CreateTuyenBayDTO(tuyenbay);
                    listtuyenbay.Add(tuyenbayDTO);
                }
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            return listtuyenbay;
        }
        public TuyenBay_DTO GetTuyenBayDetails(string mMaTuyenBay)
        {
            TuyenBay_DTO tuyenbayDTO = new TuyenBay_DTO();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from tb in context.TuyenBays
                            where tb.MaTuyenBay == mMaTuyenBay
                            select tb;
                TuyenBay tuyenbayEF = query.First();
                tuyenbayDTO = CreateTuyenBayDTO(tuyenbayEF);
            }
            catch(System.Exception)
            {}
            return tuyenbayDTO;
        }
        public static string MaTuyenBayMax()
        {
            QLCBDataContext context = new QLCBDataContext();
            string query = (from tb in context.TuyenBays select tb.MaTuyenBay).Max();
            return query;
        }

        public void InsertTuyenBay(TuyenBay_DTO tuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                TuyenBay tuyenbay = new TuyenBay();
                tuyenbay.MaTuyenBay = tuyenbayDTO.MaTuyenBay;
                tuyenbay.SanBayDi = tuyenbayDTO.SanBayDi;
                tuyenbay.SanBayDen = tuyenbayDTO.SanBayDen;
                context.TuyenBays.InsertOnSubmit(tuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void UpdateTuyenBay(TuyenBay_DTO tuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                TuyenBay tuyenbay = new TuyenBay();
                tuyenbay = context.TuyenBays.Single(tb => tb.MaTuyenBay == tuyenbayDTO.MaTuyenBay);
                tuyenbay.MaTuyenBay = tuyenbayDTO.MaTuyenBay;
                tuyenbay.SanBayDi = tuyenbayDTO.SanBayDi;
                tuyenbay.SanBayDen = tuyenbayDTO.SanBayDen;
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void DeleteTuyenBay(TuyenBay_DTO TuyenBayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                TuyenBay tuyenbay = new TuyenBay();
                tuyenbay = context.TuyenBays.Single(tb => tb.MaTuyenBay == TuyenBayDTO.MaTuyenBay);
                context.TuyenBays.DeleteOnSubmit(tuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }
    }
}
